<?php

/*
 * B端操作功能模块
 * 功能：评价
 */

namespace Home\Controller;

use Think\B_Controller;

class EvaluateController extends B_Controller {
	    
        CONST READ_STATUS = 2;  // 已读
        CONST UNREAD_STATUS = 1; //未读
	public function __construct() {
        parent::__construct();
        $this->e = D('Evaluate','Logic');
    }


    public function index(){
        $store_id = (int)$_GET['store_id'];
        if(!in_array($store_id,$this->store_id)){
            exit('非法操作！');
        }
        $item_id = $_GET['id'];
        $it = new \Home\Logic\ShopitemLogic();
        $item = $it->getItemById($item_id,$store_id,true);
        $evaluates = $this->e->appendEvaluate($item['item_number'],$store_id,0);
        //用户信息
        $u = new \Home\Logic\UserLogic();
        foreach($evaluates as $kk => $vv){
            $evaluates[$kk]['user'] = $u->getUserById($vv['client_id']);
        }

        $this->assign('item',$item);
        $this->assign('evaluates',$evaluates);
        $this->display('index');
    }

    public function append(){
       $store_id = (int)$_GET['store_id'];
       $item_number = $_GET['item_number'];
       $start = (int)$_GET['start'];
       $evaluates = $this->e->appendEvaluate($item_number,$store_id,$start);
       //用户信息
        $u = new \Home\Logic\UserLogic();
        foreach($evaluates as $kk => $vv){
            $evaluates[$kk]['user'] = $u->getUserById($vv['client_id']);
        }
        if(count($evaluates)<1){
            exit('false');
        }else{
            $evaluates = json_encode($evaluates);
            echo $evaluates;
            exit;
        }
        

    }

	//回复提交
    public function reply_submit(){
        $data['b_user_id'] = $_SESSION['id'];
        if($data['b_user_id']<1){

        }
        $data['store_id'] = (int)$_GET['store_id'];
        $data['content'] = $_POST['content'];
        $data['order_number'] = $_POST['order_number'];
        $data['evaluate_id'] = (int)$_POST['evaluate_id'];
        $data['item_number'] = $_POST['item_number'];
        $data['item_id'] = (int)$_POST['item_id'];
        $data['client_id'] = $_POST['client_id'];
        $res = $this->e->checkEvaluate($data['item_number'],$data['evaluate_id'],$data['order_number'],$data['store_id']); 
        if($data['store_id']<1 || 
            strlen($data['content'])<5 || 
            $data['evaluate_id']<1 || 
            $data['order_number']<1 ||
            strlen($data['item_number'])<1 ||
            $data['item_id']<1 ||
            $data['client_id']<1 || 
            $res){

            exit('false');
        }
        
        $data['star_level'] = 0;
        $data['status'] = 1; 
        $data['create_time'] = time(); 
        $re = $this->e->insertEvaluate($data);
        if($re){
            exit('true');
        }else{
            exit('false');
        }
    }

    

}